<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            /* 隐藏body窗口区域滚动条 */
        }
    </style>
    <!--引入three.js三维引擎-->
    <script src="/build/three.js"></script>
    <!-- 引入threejs扩展控件OrbitControls.js -->
    <script src="/examples/js/controls/OrbitControls.js"></script>
    <!-- 引入obj模型加载库OBJLoader.js -->
    <script src="/examples/js/loaders/OBJLoader.js"></script>
    <!-- 引入obj模型材质加载库MTLLoader.js -->
    <script src="/examples/js/loaders/MTLLoader.js"></script>
</head>

<body>
    <script>
        /**
         * 创建场景对象Scene
         */
        var scene = new THREE.Scene()
        // scene.background = new THREE.Color().setHSL(0.6, 0, 1);
        // scene.fog = new THREE.Fog(scene.background, 1, 5000);
        /**
         * OBJ文件加载  只加载obj文件中的几何信息，不加载材质文件.mtl
         */
        var OBJLoader = new THREE.OBJLoader()
        var MTLLoader = new THREE.MTLLoader();
        MTLLoader.setPath('model/');
        MTLLoader.load('d.mtl', function (materials) {
            console.log(materials.materials)
            materials.preload();
            var objLoader = new THREE.OBJLoader();
            objLoader.setMaterials(materials);
            objLoader.setPath('model/');
            objLoader.load('d.obj', function (obj) {

                var img_arr = ['Beijing_Roughness.png', 'Louti_BaseColor.png', 'Beijing_BaseColor.png', 'Louti_BaseColor.png'];
                img_arr.forEach((item, i) => {
                    obj.children[0].material[i] = new THREE.MeshPhongMaterial({
                        // 基础Basic网格材质，不受光照影响   Phong网格材质受光照影响
                        map: new THREE.TextureLoader().load('./img/' + item), //设置颜色纹理贴图
                        shininess: 100
                    })

                    obj.children[0].material[i].alphaMap = new new THREE.TextureLoader().load('./img/' + item);
                    // obj.children[0].specularMap = true;
                })

                // obj.children[0].material[0] = new THREE.MeshBasicMaterial({
                //         // 基础Basic网格材质，不受光照影响   Phong网格材质受光照影响
                //         map: new THREE.TextureLoader().load('./img/Louti_BaseColor.png'), //设置颜色纹理贴图
                //     })
                obj.traverse(function (child) {
                    if (child.isMesh) {
                        child.castShadow = true;
                        child.receiveShadow = true;
                    }
                });
                obj.position.y = 100;
                obj.scale.x = 2;
                obj.scale.y = 2;
                obj.scale.z = 2;
                obj.rotation.y = - Math.PI / 2;
                scene.add(obj);

                console.log(obj) //加载纹理贴图
            });
        });
        /**
         * 光源设置
         */
        //点光源
        var point = new THREE.PointLight(0xffffff);
        point.position.set(400, 200, 300); //点光源位置
        // scene.add(point); //点光源添加到场景中
        //环境光
        var ambient = new THREE.AmbientLight(0x444444);
        // scene.add(ambient);

        var hemiLight = new THREE.HemisphereLight(0xddeeff, 0x0f0e0d, 0.1);
        scene.add(hemiLight);

        var dirLight = new THREE.DirectionalLight(0xffffff, 1);
        dirLight.color.setHSL(0.1, 50, 0.95);
        dirLight.position.set(- 1, 100, 1);
        dirLight.position.multiplyScalar(30);
        // scene.add(dirLight);
        dirLight.castShadow = true;
        dirLight.shadow.mapSize.width = 2048;
        dirLight.shadow.mapSize.height = 2048;
        var d = 50;
        dirLight.shadow.camera.left = - d;
        dirLight.shadow.camera.right = d;
        dirLight.shadow.camera.top = d;
        dirLight.shadow.camera.bottom = - d;
        dirLight.shadow.camera.far = 3500;
        dirLight.shadow.bias = - 0.0001;

        // var groundGeo = new THREE.PlaneBufferGeometry(10000, 10000);
        // var groundMat = new THREE.MeshLambertMaterial({ color: 0xffffff });
        // groundMat.color.setHSL(0.095, 1, 0.75);
        // var ground = new THREE.Mesh(groundGeo, groundMat);
        // ground.position.y = - 33;
        // ground.rotation.x = - Math.PI / 2;
        // ground.receiveShadow = true;
        // scene.add(ground);

        var floorMat = new THREE.MeshStandardMaterial({
            roughness: 0.8,
            color: 0xFF7F24,
            metalness: 0.2,
            bumpScale: 0.0005
        });

        var floorGeometry = new THREE.PlaneBufferGeometry(500, 500);
        var floorMesh = new THREE.Mesh(floorGeometry, floorMat);
        floorMesh.receiveShadow = true;
        floorMesh.rotation.x = - Math.PI / 2.0;
        // scene.add(floorMesh);

        // GROUND
        var groundGeo = new THREE.PlaneBufferGeometry(10000, 10000);
        var groundMat = new THREE.MeshLambertMaterial({ color: 0xffffff });
        groundMat.color.setHSL(0.095, 1, 0.75);
        var ground = new THREE.Mesh(groundGeo, groundMat);
        ground.position.y = - 33;
        ground.rotation.x = - Math.PI / 2;
        // ground.receiveShadow = true;
        scene.add(ground);


        var bulbGeometry = new THREE.SphereBufferGeometry(0.01, 16, 8);
        var bulbLight = new THREE.PointLight(0xffee88, 1, 300, 1);
        var bulbMat = new THREE.MeshStandardMaterial({
            emissive: 0xffffee,
            emissiveIntensity: 1,
            color: 0x000000
        });
        bulbLight.add(new THREE.Mesh(bulbGeometry, bulbMat));
        bulbLight.castShadow = true;
        scene.add(bulbLight);
        dirLight.position.set(100, 100, -70);

        bulbLight.position.set(100, 100, -50);
        hemiLight.position.set(0, 200, 0);
        var dirLightHeper = new THREE.HemisphereLightHelper(hemiLight, 5);
        // scene.add(dirLightHeper);
        /**
         * 相机设置
         */
        var width = window.innerWidth; //窗口宽度
        var height = window.innerHeight; //窗口高度
        var k = width / height; //窗口宽高比
        var s = 150; //三维场景显示范围控制系数，系数越大，显示的范围越大
        //创建相机对象
        //var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
        var camera = new THREE.PerspectiveCamera(75,
            window.innerWidth / window.innerHeight, 1, 1000);
        camera.position.set(200, 300, 200); //设置相机位置
        camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
        /**
         * 创建渲染器对象
         */
        var renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.gammaInput = true;
        renderer.gammaOutput = true;
        renderer.shadowMap.enabled = true;
        renderer.setPixelRatio(window.devicePixelRatio);
        renderer.setSize(width, height); //设置渲染区域尺寸
        renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
        document.body.appendChild(renderer.domElement); //body元素中插入canvas对象

        var i = 0;
        var isadd = true;

        // 渲染函数
        function render() {

            // bulbLight.position.y += 0.1;

            if (i > 200 || !isadd) {
                isadd = false;
                bulbLight.position.z -= 0.5;
                i--;
                if (i <= 0) {
                    isadd = true;
                }
            }
            else if (isadd) {
                bulbLight.position.z += 0.5;
                i++;
            }

            // dirLight.position.y -= 0.2;

            //bulbLight.position.set(20, 100, -70);

            renderer.render(scene, camera); //执行渲染操作
            // mesh.rotateY(0.01);//每次绕y轴旋转0.01弧度
            requestAnimationFrame(render); //请求再次执行渲染函数render，渲染下一帧


        }
        render();
        //创建控件对象  相机对象camera作为参数   控件可以监听鼠标的变化，改变相机对象的属性
        var controls = new THREE.OrbitControls(camera);


        //监听鼠标事件，触发渲染函数，更新canvas画布渲染效果
        // controls.addEventListener('change', render);
        function color16() { //十六进制颜色随机
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            var color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
            return color
        }
    </script>
</body>

</html>